﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace de.codingfreaks.libs.cfThreadingTools
{
    internal class Tools
    {

        internal static PropertyInfo GetPropertyObject(object objOwner,
                                                       string strPropertyName)
        {
            Type typThis = objOwner.GetType();
            return typThis.GetProperty(strPropertyName);            
        }

        internal static void SetPropertyValue(object objOwner,
                                              string strPropertyName,
                                              object objValue,
                                              object[] aobjIndizes)
        {
            PropertyInfo pinfThis = GetPropertyObject(objOwner, strPropertyName);
            if (pinfThis != null)
            {
                try
                {
                    pinfThis.SetValue(objOwner, objValue, aobjIndizes);
                }
                catch (Exception excThis)
                { 
                
                }
            }
        }        

        internal static MethodInfo GetMethodObject(object objOwner,
                                                   string strMethodName,
                                                   params object[] aobjParams)
        {
            Type typThis = objOwner.GetType();
            Type[] atypParams = null;
            if (aobjParams.Length == 0)
            {
                atypParams = System.Type.EmptyTypes;
            }
            else
            {
                atypParams = new Type[aobjParams.Length];
                int i = 0;
                foreach (object objParam in aobjParams)
                {
                    atypParams[i] = objParam.GetType();
                    i++;
                }
            }
            return typThis.GetMethod(strMethodName, atypParams);
        }

        internal static object CallMethod(object objOwner,
                                        string strMethodName,
                                        params object[] aobjParams)
        {
            MethodInfo minfThis = GetMethodObject(objOwner, strMethodName, aobjParams);
            if (minfThis != null)
            {
                try
                {
                    return minfThis.Invoke(objOwner, aobjParams);                    
                }
                catch (Exception excThis)
                { 
                
                }
            }
            return null;
        }

    }
}
